#!/bin/sh

# Slackware build script for cracklib
# Written by Christopher Walker <kris240376@gmail.com>

PRGNAM=cracklib
VERSION=${VERSION:-2.8.13}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

DICT=${DICT:-cracklib-words-20080507}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R a-s,u+w,go+r-w .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --enable-shared \
  --disable-static \
  --build=$ARCH-slackware-linux

make
make install DESTDIR=$PKG

# Install large cracklib dictionary file if it exists
( cd $PKG/usr/share/cracklib
  if [ -e $CWD/${DICT}.gz ]; then
     zcat $CWD/${DICT}.gz > $PRGNAM-large
  elif [ -e $CWD/${DICT} ]; then
    cat $CWD/${DICT} > $PRGNAM-large
  fi
)

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null
)

#find $PKG/usr/man -type f -exec gzip -9 {} \;

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  ABOUT-NLS AUTHORS COPYING INSTALL NEWS README* \
    $PKG/usr/doc/$PRGNAM-$VERSION
chmod 644 $PKG/usr/doc/$PRGNAM-$VERSION/*

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
